package com.example.spring.mono.controller;

import com.example.spring.mono.entity.EleUser;
import com.example.spring.mono.repository.EleUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/eleUser")
public class EleUserController {
    @Autowired
    private EleUserRepository eleUserRepository;
 /*   @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;*/

   @PostMapping("/add")
    public Mono<ResponseEntity<EleUser>> addEleUser(@RequestBody EleUser user){
        //直接将数据保存到es中，索引名称为user-demo，在user对象中设置的
        //通过kibana进行查询GET /user-demo/_search
        EleUser save = eleUserRepository.save(user);
        ResponseEntity<EleUser> successponseEntity = new ResponseEntity(save,HttpStatus.OK);
        ResponseEntity<EleUser> errorponseEntity = new ResponseEntity(HttpStatus.FAILED_DEPENDENCY);
        if (null != save){
            return Mono.just(successponseEntity);
        }else {
         return    Mono.empty();
        }
    }
    @GetMapping("/find")
    public Flux findAll(){
        //模糊查询。精确查询
        Iterable<EleUser> all = eleUserRepository.findAll();
        List<EleUser> list = new ArrayList<>();
        for (EleUser user : all) {
            list.add(user);
        }
        if (null!= list && list.size()>0){
          return Flux.fromArray(list.toArray());
        }else {
            return   Flux.fromArray(list.toArray());
        }
    }

}
